# coding: utf-8

import rediscluster
from lib.redis.abstract import RedisAbstract


class RedisCluster(RedisAbstract):

    def __init__(self, nodes, auth=None, decode_responses=False, logger=None):
        super(RedisCluster, self).__init__(logger)
        startup_nodes = [{'host': host, 'port':port} for (host, port) in nodes]
        self.cluster = rediscluster.StrictRedisCluster(
            startup_nodes=startup_nodes, password=auth,
            decode_responses=decode_responses)

    def ping(self):
        return self.cluster.ping()

    def size(self):
        return self.cluster.dbsize()

    def ttl(self, name):
        return self.cluster.ttl(name)

    def expire(self, name, time):
        return self.cluster.expire(name, time)

    def persist(self, name):
        return self.cluster.persist(name)

    def type(self, name):
        return self.cluster.type(name)

    def rename(self, name, new_name):
        return self.cluster.rename(name, new_name)

    def renamenx(self, name, new_name):
        return self.cluster.renamenx(name, new_name)

    def keys(self, pattern='*'):
        return self.cluster.keys(pattern=pattern)

    def scan(self, cursor=0, match=None, count=None):
        return self.cluster.scan(cursor, match, count)

    def exists(self, name):
        return self.cluster.exists(name)

    def get(self, name):
        return self.cluster.get(name)

    def set(self, name, value):
        return self.cluster.set(name, value)

    def getset(self, name, value):
        return self.cluster.getset(name, value)

    def delete(self, name):
        return self.cluster.delete(name)

    def hexists(self, name, key):
        return self.cluster.hexists(name, key)

    def hall(self, name):
        return self.cluster.hgetall(name)

    def hget(self, name, key):
        return self.cluster.hget(name, key)

    def hset(self, name, key, value):
        return self.cluster.hset(name, key, value)

    def hmget(self, name, keys, *args):
        return self.cluster.hmget(name, keys, args)

    def hmset(self, name, dict):
        return self.cluster.hmset(name, dict)

    def hdel(self, name, key):
        return self.cluster.hdel(name, key)



# if __name__ == '__main__':
#     nodes = [('10.168.2.39', 7000 + i) for i in range(6)]
#     facotry = RedisCluster(nodes, '123456@')
#     keys = facotry.keys('*')
#     for key in keys:
#         type_name = facotry.type(key)
#         # value = facotry.get(key)
#         print('%s: %s' % (type_name, key.decode('iso-8859-1')))

